package com.yzt.message.custom;


import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Keep;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.yzt.zhuju.MainMenuContsant;
import com.yzt.zhuju.utils.SpannableUtils;
import com.yzt.zhuju.utils.UrlUtil;
import com.yzt.message.R;

import java.util.List;
import java.util.Map;

import io.rong.imkit.conversation.extension.component.emoticon.AndroidEmoji;
import io.rong.imkit.model.UiMessage;
import io.rong.imkit.widget.adapter.IViewProviderListener;
import io.rong.imlib.model.MessageContent;

/**
 * 系统自定义消息
 */
@Keep
public class RXCustomTipMessageItemProvider extends CustomBaseMessageItemProvider<RXCustomTipMessageContent> {

    public RXCustomTipMessageItemProvider() {
        mConfig.showReadState = false;
        mConfig.showPortrait = false;
        mConfig.showContentBubble = false;
        mConfig.centerInHorizontal = true;
    }

    @Override
    protected io.rong.imkit.widget.adapter.ViewHolder onCreateMessageContentViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.nomi_rc_item_custom_tip_text_message, viewGroup, false);
        return new ViewHolder(view.getContext(), view);
    }

    @Override
    protected void bindMessageContentViewHolder(io.rong.imkit.widget.adapter.ViewHolder viewHolder, io.rong.imkit.widget.adapter.ViewHolder viewHolder1, RXCustomTipMessageContent content, UiMessage data, int i, List<UiMessage> list, IViewProviderListener<UiMessage> iViewProviderListener) {
        try {
            ViewHolder holder = (ViewHolder) viewHolder;

            holder.ivTipBell.setImageResource(R.mipmap.icon_message_tip_bell);

            String contentStr = content.getContent();
            if (TextUtils.isEmpty(contentStr)) {
                return;
            }
            if (MainMenuContsant.isLimitConsumption() && contentStr.startsWith("为表诚意")) {
                contentStr = "聊的开心就关注对方哟！";
            }
            holder.message.setText(contentStr);

            Map<String, Object> extra = content.getMyExtra();
            if (extra != null && extra.size() > 0) {

                if (extra.containsKey("contentHighlights")) {
                    String str = JSON.toJSONString(extra.get("contentHighlights"));

                    List<ContentHighlight> contentHighlights = JSON.parseArray(str, ContentHighlight.class);

                    if (contentHighlights == null || contentHighlights.size() <= 0) {
                        return;
                    }

                    contentStr = contentStr.replaceAll("\\n", "<br/>");
                    for (ContentHighlight light : contentHighlights) {
                        String path = light.getJumpAndroidPath();
                        if (path == null) {
                            path = "";
                        }
                        if (path.startsWith(UrlUtil.TYPE_ROUTER_INVITE_AUTH)) {
                            path = path + "?uid=" + data.getTargetId();
                        }
                        contentStr = contentStr.replace(light.getHighlightContent(), "<a href='" + path + "'>" + light.getHighlightContent() + "</a>");
                    }

                    SpannableUtils.setTipClick(holder.message, contentStr, Color.parseColor("#FF6550"), false, true);
                }
            }
        } catch (Exception e) {

        }
    }

    @Override
    protected boolean onItemClick(io.rong.imkit.widget.adapter.ViewHolder viewHolder, RXCustomTipMessageContent rxCustomTipMessageContent, UiMessage uiMessage, int i, List<UiMessage> list, IViewProviderListener<UiMessage> iViewProviderListener) {
        return false;
    }

    @Override
    protected boolean isMessageViewType(MessageContent messageContent) {
        return messageContent instanceof RXCustomTipMessageContent && !messageContent.isDestruct();
    }

    @Override
    public Spannable getSummarySpannable(Context context, RXCustomTipMessageContent data) {
        if (data == null) {
            return null;
        }
        String content = data.getContent();
        if (content != null) {
            if (content.length() > 100) {
                content = content.substring(0, 100);
            }
            return new SpannableString(AndroidEmoji.ensure(content));
        }

        return null;
    }

    private static class ViewHolder extends io.rong.imkit.widget.adapter.ViewHolder {
        TextView message;
        ImageView ivTipBell;

        public ViewHolder(Context context, View view) {
            super(context, view);

            this.message = view.findViewById(R.id.tv_content);
            this.ivTipBell = view.findViewById(R.id.iv_tip_bell);
        }
    }

    @Keep
    public static class ContentHighlight {

        private String jumpRoute;
        @JSONField(name = "HighlightContent")
        private String highlightContent;

        public String getJumpRoute() {
            return jumpRoute;
        }

        public void setJumpRoute(String jumpRoute) {
            this.jumpRoute = jumpRoute;
        }

        public String getHighlightContent() {
            return highlightContent;
        }

        public void setHighlightContent(String highlightContent) {
            this.highlightContent = highlightContent;
        }

        public String getJumpAndroidPath() {
            if (TextUtils.isEmpty(jumpRoute)) {
                return "";
            }
            Map<String, String> map = JSON.parseObject(jumpRoute, Map.class);
            if (map != null) {
                return map.get("androidPath");
            }
            return "";
        }
    }
}

